//
//  DYJTapScrollViewViewController.m
//  Piece
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "DYJTapScrollViewViewController.h"
#import "DYJContentsTableViewCell.h"
#import "ZHNetworkHandler.h"
#import "DYJContentsModel.h"
#import "DYJShortViewController.h"
#import "DYJSerialContentModel.h"
#import "DYJSerialViewController.h"
#import "DYJQuestionViewController.h"
#import "FeHourGlass.h"
#import "ZHArchiverTools.h"
@interface DYJTapScrollViewViewController ()<UITableViewDelegate, UITableViewDataSource>
//目录
@property (nonatomic, strong) UITableView *contentsTableView;
//目录数据源数组
@property (nonatomic, strong) NSMutableArray *contentsDataArray;
@property (strong, nonatomic) FeHourGlass *hourGlass;
@property (nonatomic, strong) UIImageView *returnImageView;
@property (nonatomic, strong) UIImageView *grayReturnImageView;
@end

@implementation DYJTapScrollViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitle];
    self.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    //获取背景颜色
    [self getBackgroundColor];
//    self.navigationController.navigationBar.barTintColor = self.view.backgroundColor;
    //创建返回按钮
    [self createReturnButton];
    //创建目录tableView
    [self createContentsTableView];
    //获取目录数据
    [self getContentsData];
}
#pragma mark -- 返回按钮
- (void)createReturnButton
{
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return"] style:UIBarButtonItemStylePlain target:self action:@selector(didTapReturnImageView)];
    _returnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 40, 40)];
    _returnImageView.image = [UIImage imageNamed:@"close_normal_white"];
    [self.view addSubview:_returnImageView];
    //添加点击事件
    _returnImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapReturnImageView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapReturnImageView)];
    [_returnImageView addGestureRecognizer:tapReturnImageView];
    
    _grayReturnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 40, 40)];
    _grayReturnImageView.image = [UIImage imageNamed:@"close_normal"];
    [self.view addSubview:_grayReturnImageView];
    //添加点击事件
    _grayReturnImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGrayReturnImageView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapReturnImageView)];
    [_grayReturnImageView addGestureRecognizer:tapGrayReturnImageView];
}
#pragma mark -- 添加标题
- (void)createTitle
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 20, ScreenWidth - 120, 40)];
    titleLabel.text = _readTitle;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = 1;
    [self.view addSubview:titleLabel];
}
- (void)didTapReturnImageView
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- 背景色
- (void)getBackgroundColor
{
    switch ((long)_colorNum) {
        case 1:
            self.view.backgroundColor = [UIColor colorWithRed:0.95 green:0.82 blue:0.49 alpha:1.00];
            break;
        case 2:
            self.view.backgroundColor = [UIColor colorWithRed:0.51 green:0.81 blue:0.64 alpha:1.00];
            break;
        case 3:
            self.view.backgroundColor = [UIColor colorWithRed:0.60 green:0.45 blue:0.34 alpha:1.00];
            break;
        case 4:
            self.view.backgroundColor = [UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:1.00];
            break;
        case 5:
            self.view.backgroundColor = [UIColor colorWithRed:0.82 green:0.69 blue:0.43 alpha:1.00];
            break;
        case 6:
            self.view.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.00];
            break;
        case 7:
            self.view.backgroundColor = [UIColor colorWithRed:0.99 green:0.75 blue:0.67 alpha:1.00];
            break;
        case 8:
            self.view.backgroundColor = [UIColor colorWithRed:0.67 green:0.91 blue:0.93 alpha:1.00];
            break;
        case 9:
            self.view.backgroundColor = [UIColor colorWithRed:0.51 green:0.81 blue:0.98 alpha:1.00];
            break;
        case 10:
            self.view.backgroundColor = [UIColor colorWithRed:0.83 green:0.73 blue:0.64 alpha:1.00];
            break;
        default:
            self.view.backgroundColor = [UIColor colorWithRed:0.51 green:0.81 blue:0.64 alpha:1.00];
            break;
    }
}
#pragma mark -- 初始化tableView
- (void)createContentsTableView
{
    self.contentsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
    self.contentsTableView.separatorStyle = 0;
    self.contentsTableView.backgroundColor = self.view.backgroundColor;
    [self.view addSubview:_contentsTableView];
    self.contentsTableView.delegate = self;
    self.contentsTableView.dataSource = self;
    
    //注册cell
    [self.contentsTableView registerClass:[DYJContentsTableViewCell class] forCellReuseIdentifier:@"contentsCell"];
}
- (void)myTask
{
    // Do something usefull in here instead of sleeping ...
    sleep(6);
}

#pragma mark -- 数据解析
- (void)getContentsData
{
    
    _hourGlass = [[FeHourGlass alloc] initWithView:self.view];
    [self.view addSubview:_hourGlass];
    [self.view bringSubviewToFront:_grayReturnImageView];
    [_hourGlass showWhileExecutingBlock:^{
        [self myTask];
    } completion:^{
        
        
    }];

    //初始化目录数据源数组
    self.contentsDataArray = [NSMutableArray array];
    NSString *urlStr = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/reading/carousel/%@", _id];
    [ZHNetworkHandler getDataByURLString:urlStr WithBodyString:nil WithHeaderDic:nil WithResponse:0 WithSuccess:^(id result) {
        //逐层解析
        NSDictionary *dictionary = (NSDictionary *)result;
        [ZHArchiverTools archiverObject:dictionary ByKey:@"tapScroolCache" WithPath:@"tapScroll.plist"];
        NSArray *dataArray = [dictionary valueForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            DYJContentsModel *contentsModel = [[DYJContentsModel alloc] initWithDic:dic];
            [self.contentsDataArray addObject:contentsModel];
        }
        [self.contentsTableView reloadData];
        [_hourGlass removeFromSuperview];
        _grayReturnImageView.hidden = YES;
    } WithFailure:^(NSError *error) {
        NSDictionary *dictionary = [ZHArchiverTools unarchiverObjectByKey:@"tapScroolCache" WithPath:@"tapScroll.plist"];
        if (dictionary == nil) {
           
        }
        else
        {
            NSArray *dataArray = [dictionary valueForKey:@"data"];
            for (NSDictionary *dic in dataArray) {
                DYJContentsModel *contentsModel = [[DYJContentsModel alloc] initWithDic:dic];
                [self.contentsDataArray addObject:contentsModel];
            }
            [self.contentsTableView reloadData];
            [_hourGlass removeFromSuperview];
            _grayReturnImageView.hidden = YES;

        }
    }];
}
#pragma mark -- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _contentsDataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJContentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"contentsCell"];
    DYJContentsModel *contentsModel = [_contentsDataArray objectAtIndex:indexPath.row];
    cell.model = contentsModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJContentsModel *contentsModel = [_contentsDataArray objectAtIndex:indexPath.row];
    if (contentsModel.type == 1) {
        DYJShortViewController *svc = [[DYJShortViewController alloc] init];
        svc.shortId = contentsModel.item_id;
        [self.navigationController pushViewController:svc animated:YES];
    }
    else if (contentsModel.type == 2)
    {
        DYJSerialViewController *sevc = [[DYJSerialViewController alloc] init];
        sevc.serialId = contentsModel.item_id;
        [self.navigationController pushViewController:sevc animated:YES];
    }
    else
    {
        DYJQuestionViewController *qvc = [[DYJQuestionViewController alloc] init];
        qvc.questionId = contentsModel.item_id;
        [self.navigationController pushViewController:qvc animated:YES];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;

    //self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
